package com.gem.service.impl;

import com.gem.domain.Teacher;
import com.gem.mapper.TeacherMapper;
import com.gem.service.TeacherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.NoTransactionException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Bravea❤ ·勇
 * Description: 授课人信息service
 * create
 * Modified By:
 */
@Service
@Transactional
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public boolean addTeacher(Teacher t) {
        try {
            if (t.getTMoney() == null){
                t.setTMoney(0.0);
            }
            return teacherMapper.addTeacher(t);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Teacher selectTeacherById(String tId) {
        try {
            return teacherMapper.selectTeacherById(tId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //模糊查询老师(Apolar_sh)
    @Override
    public List<Teacher> findTeacherByName(String ccrcCourseTeacher) {
        return teacherMapper.selectTeacherByName(ccrcCourseTeacher);
    }

    @Override
    public boolean deleteTeacherById(String[] tId) {
        try {
            boolean isDeleteSuccess = true;
            for (String t: tId) {
                if (t != null && !t.trim().equals("")){
                    if(!teacherMapper.deleteTeacherById(t)){
                        isDeleteSuccess = false;
                        break;
                    }
                }
            }
            if(isDeleteSuccess){
                return true;
            }else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public List<Teacher> selectAllTeacher() {
        try {
            return teacherMapper.selectAllTeacher();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean modifyTeacherById(Teacher t) {
        try {
            if (t.getTMoney() == null){
                t.setTMoney(0.0);
            }
            return teacherMapper.modifyTeacherById(t);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public PageInfo<Teacher> selectTeacherByPage(int currentPage, int pageSize) {
        PageInfo<Teacher> pageInfo = null;
        try {
            PageHelper.startPage(currentPage, pageSize);
            pageInfo = new PageInfo<>(teacherMapper.selectAllTeacher());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Teacher> searchTeacherByPage(int currentPage, int pageSize, String tName, String tMajor) {
        PageHelper.startPage(currentPage, pageSize);
        PageInfo<Teacher> pageInfo = null;
        try {
            tName = "%" + tName + "%";
            tMajor = "%" + tMajor +"%";
            pageInfo = new PageInfo<>(teacherMapper.searchTeacher(tName,tMajor));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public boolean addTeacherList(List<Teacher> list) {
        try {
            boolean is = true;
            for (Teacher t : list) {
                if (t.getTMoney() == null){
                    t.setTMoney(0.0);
                }
                if(!teacherMapper.addTeacher(t)){
                    is = false;
                }
            }
            if (is){
                return true;
            }else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (NoTransactionException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return false;
    }

    @Override
    public Teacher selectByName(String tName) {
        return teacherMapper.selectTeacherByName(tName).get(0);
    }
}
